package cn.wp.ewallet.wmps.dao.impl;

import cn.hutool.core.date.DateTime;
import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wmps.dao.EarnStatLogDao;
import cn.wp.ewallet.wmps.model.entity.EarnStatLogDo;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 收益统计-数据访问层实现
 * Created by zhnagyongli on 2020/01/11
 */
@Repository
public class EarnStatLogDaoImpl implements EarnStatLogDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public EarnStatLogDo selectLatestEarnStatLog(String address) {
        SqlString sqlString = new SqlString();
        sqlString.add("select * from earn_stat_log e where e.wallet_address = ? order by e.stat_date desc limit 1");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[] {address}, EarnStatLogDo.class);
    }

    @Override
    public EarnStatLogDo selectEarnStatLogByDate(String address, String date) {
        SqlString sqlString = new SqlString();
        sqlString.add("select * from earn_stat_log e where e.wallet_address = ? and e.stat_date = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[] {address, date}, EarnStatLogDo.class);
    }

    @Override
    public void update(EarnStatLogDo earnStatLogDo) {
        jdbcSupportTemplate.updateSelective(earnStatLogDo);
    }

    @Override
    public void insert(EarnStatLogDo earnStatLogDo) {
        jdbcSupportTemplate.insert(earnStatLogDo);
    }

    @Override
    public EarnStatLogDo selectEarnLogByStatDateAndWalletAddress(Date date, String walletAddress) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select * from earn_stat_log where stat_date = ? and wallet_address = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[]{date, walletAddress}, EarnStatLogDo.class);
    }
}